package com.example.shopping.app;

import android.animation.Animator;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.example.shopping.R;
import com.example.shopping.app.ForgetActivity;
import com.example.shopping.app.MainActivity;
import com.example.shopping.app.RegisterActivity;
import com.example.shopping.bean.Response;
import com.example.shopping.bean.User;
import com.example.shopping.home.fragment.HomeFragment;
import com.example.shopping.utils.Constants;
import com.example.shopping.utils.NbButton;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okio.BufferedSink;


public class LoginActivity extends AppCompatActivity {

    private NbButton button;
    private RelativeLayout rlContent;
    private Handler handler;
    private Animator animator;

    private TextView email;

    private TextView password;

    private String loginResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        button=findViewById(R.id.btn_login);
        rlContent=findViewById(R.id.rl_content);

        email = findViewById(R.id.email);
        password = findViewById(R.id.password);

        Drawable background = rlContent.getBackground();
        if (background != null){
            background.setAlpha(0);
        }

        handler=new Handler();

        button.setOnClickListener(v ->
        {

            new Thread(new Runnable() {
                @Override
                public void run() {
                    // 请求网络
                    try {
                        getDataFromNet();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();

            button.startAnim();

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {

                    //跳转
                    gotoNew();
                }
            },3000);

        });
    }
    public void getDataFromNet() throws IOException {

        String url = Constants.LoginController;

        String loginEmail =  email.getText().toString();
        String loginPassword = password.getText().toString();
        Log.e(loginEmail,loginPassword);

        Map<String,String> jsonString = new HashMap<>();
        jsonString.put("email",loginEmail);
        jsonString.put("password",loginPassword);

        OkHttpUtils
                .postString()
                .url(url)
                .content(new Gson().toJson(jsonString))
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("onError",e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        loginResponse = response;
                        Log.e("onResponse",loginResponse);

                    }
                });

    }




    private void gotoNew() {


        button.gotoNew();
        Intent intent;
        if (loginResponse == null){
            finish();
            intent = new Intent(LoginActivity.this,LoginActivity.class);
            startActivity(intent);
        }
        Log.e("loginResponse " ,String.valueOf(loginResponse));
        Response response = JSON.parseObject(loginResponse, Response.class);
        Log.e("response " ,String.valueOf(response));
        // 用户名或者密码错误
        if (response.getCode() == 415000){

            intent = new Intent(LoginActivity.this,LoginActivity.class);
            startActivity(intent);
            Toast.makeText(this,"用户名或密码不正确",Toast.LENGTH_SHORT).show();
        }
        else {

            User me = JSON.parseObject(String.valueOf(response.getData()), User.class);
            // 将用户信息存入全局
            MyApplication.setMe(me);
            intent = new Intent(this, MainActivity.class);
        }

        int xc=(button.getLeft()+button.getRight())/2;
        int yc=(button.getTop()+button.getBottom())/2;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            animator= ViewAnimationUtils.createCircularReveal(rlContent,xc,yc,0,1111);
        }
        animator.setDuration(300);

        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        overridePendingTransition(R.anim.anim_in,R.anim.anim_out);
                    }
                },200);
            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.start();
        rlContent.getBackground().setAlpha(255);


        startActivity(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(animator != null){
            animator.cancel();
        }

        rlContent.getBackground().setAlpha(0);
        button.regainBackground();
    }

    // 前往"忘记密码"页面
    public void toForget(View view){
        Intent intent = new Intent(this, ForgetActivity.class);
        startActivity(intent);
    }
    // 前往"注册账号"页面
    public void toRegister(View view){
        Intent intent = new Intent(this, RegisterActivity.class);
        startActivity(intent);
    }


}

